package com.fsc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fsc.entity.domain.*;
import com.fsc.entity.vo.TotalScoreInfoVO;
import com.fsc.service.TotalScoreInfoService;
import com.fsc.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
public class TotalScoreInfoServiceImpl extends ServiceImpl<TotalScoreInfoMapper, TotalScoreInfo>
		implements TotalScoreInfoService{

	@Autowired
	private TotalScoreInfoMapper totalScoreInfoMapper;

	@Override
	public List<TotalScoreInfoVO> getTotalScoreByExamId(int examsId) {
		return totalScoreInfoMapper.selectTotalScoreByExamId(examsId);
	}

	@Override
	public TotalScoreInfoVO getTotalScoreByStudentId(int examsId, Integer studentId) {
		return totalScoreInfoMapper.selectTotalScoreByExamIdandStudentId(examsId,studentId);
	}

	@Override
	public List<TotalScoreInfoVO> getTotalScoreByStuIds(int examId, List<Integer> stuIds) {
		System.out.println(examId+":"+stuIds);
		return this.baseMapper.selectTotalScoreByExamIdandStuIds(examId,stuIds);
	}

	@Override
	public void importScore(List<TotalScoreInfoVO> totalScoreInfoVOList, Integer examsId) {
		log.info("长度：" + totalScoreInfoVOList.size());
		for (TotalScoreInfoVO vo : totalScoreInfoVOList) {
			try {
				TotalScoreInfo totalScoreInfo = convertToTotalScoreInfo(vo, examsId);

				totalScoreInfoMapper.insertOrUpdate(totalScoreInfo);
			} catch (Exception e) {
				log.error("处理成绩信息时发生异常", e);
			}
		}
	}

	@Override
	public void updateById(TotalScoreInfoVO totalScoreInfoVO) {
		 this.baseMapper.updateById(totalScoreInfoVO);
	}

	@Override
	public void deleteByExamsId(Integer id) {
		this.baseMapper.deleteByExamsId(id);
	}

	private TotalScoreInfo convertToTotalScoreInfo(TotalScoreInfoVO vo, Integer examsId) {
		TotalScoreInfo totalScoreInfo = new TotalScoreInfo();
		totalScoreInfo.setStudentId(vo.getStudentId());
		totalScoreInfo.setChinese(vo.getChinese());
		totalScoreInfo.setMath(vo.getMath());
		totalScoreInfo.setEnglish(vo.getEnglish());
		totalScoreInfo.setTotalScore(vo.getTotalScore());
		totalScoreInfo.setClassRank(vo.getClassRank());
		totalScoreInfo.setGradeRank(vo.getGradeRank());
		totalScoreInfo.setTime(LocalDate.now());
		totalScoreInfo.setExamsId(examsId);
		return totalScoreInfo;
	}
}
